﻿using System.Collections.Generic;
using System;
using System.Text;

namespace Ex03.GarageLogic
{
    /// <summary>
    /// This class is for a Motorcycle Data
    /// </summary>
    public class MotorcycleData
    {
        public enum eLicenseType
        {
            A, 
            A1, 
            A2, 
            B
        }        
        
        private const string k_LicenseType = "License Type";
        private const string k_EnginCapacityKey = "Engine Capacity";
        public const int k_NumberOfWheels = 2;
        public const float k_MaxAirPressureOfWheels = 32;
        private static readonly Dictionary<string, string> sr_MotorcycleDataForm;

        private eLicenseType m_LicenseType;
        private int m_EnginCapacity;

        /// <summary>
        /// This is a static constructor that will initialize the Motorcycle Data
        /// form 
        /// </summary>
        static MotorcycleData()
        {
            sr_MotorcycleDataForm = Wheel.CreateWheelsForm(k_NumberOfWheels);
            sr_MotorcycleDataForm.Add(k_LicenseType, null);
            sr_MotorcycleDataForm.Add(k_EnginCapacityKey, null);
        }

        /// <summary>
        /// A constructor for the Car Data
        /// </summary>
        /// <param name="i_CreationForm">
        /// A key value Dictionary where key is the name of the member 
        /// to initialize and value is the new value
        /// <remarks>
        ///     The input form Must have a license type property and an engine capacity property
        ///     the input Must be "pars-able" to eLicenseType and of int
        ///     else will throw FormatException
        /// </remarks>
        /// </param>
        public MotorcycleData(Dictionary<string, string> i_CreationForm)
        {
            initailizeLicenseTypeByForm(i_CreationForm);
            initailizeEnginCapacityByForm(i_CreationForm);
        }

        private void initailizeLicenseTypeByForm(Dictionary<string, string> i_CreationForm)
        {
            const bool v_IgnorCase = true;
            bool isGetValueSucceded;
            string licenseTypeString;
            eLicenseType licenseTypeTemp;

            isGetValueSucceded = i_CreationForm.TryGetValue(k_LicenseType, out licenseTypeString);
            if (!isGetValueSucceded)
            {
                throw new FormatException("License Type is invalid");
            }

            try
            {
                licenseTypeTemp = (eLicenseType)Enum.Parse(typeof(eLicenseType), licenseTypeString, v_IgnorCase);
            }
            catch (Exception exp)
            {
                throw new FormatException("License Type is invalid");
            }

            m_LicenseType = licenseTypeTemp;
        }

        private void initailizeEnginCapacityByForm(Dictionary<string, string> i_CreationForm)
        {
            bool isGetValueSucceded;
            bool isParseSucceded;
            string enginCapacityString;
            int enginCapacityTemp;

            isGetValueSucceded = i_CreationForm.TryGetValue(k_EnginCapacityKey, out enginCapacityString);
            isParseSucceded = int.TryParse(enginCapacityString, out enginCapacityTemp);
            if (!isGetValueSucceded || !isParseSucceded)
            {
                throw new FormatException("Engine capacity is invalid");
            }

            if (enginCapacityTemp < 0)
            {
                throw new ArgumentException("Engine capacity is invalid (below zero)");

            }

            m_EnginCapacity = enginCapacityTemp;
        }

        /// <summary>
        /// A property to get the Motorcycle Data form
        /// <remarks>will return a new form</remarks>
        /// </summary>
        public static Dictionary<string, string> MotorcycleDataForm
        {
            get { return new Dictionary<string, string>(sr_MotorcycleDataForm); }
        }

        /// <summary>
        /// Return a string representation object members as pair 
        /// members name and variable value
        /// </summary>
        /// <returns>string "varName + Vehicle.k_ToStirngPairDelimiter + varValue + Vehicle.sr_ToStirngEndOfPairDelimiter"</returns>
        public override string ToString()
        {
            StringBuilder stringBuilderResult = new StringBuilder();

            stringBuilderResult.Append(licenseTypeToString());
            stringBuilderResult.Append(enginCapacityToString());

            return stringBuilderResult.ToString();
        }

        private string licenseTypeToString()
        {
            StringBuilder stringBuilderResult = new StringBuilder();

            stringBuilderResult.Append(k_LicenseType);
            stringBuilderResult.Append(Vehicle.k_ToStirngPairDelimiter);
            stringBuilderResult.Append(m_LicenseType);
            stringBuilderResult.Append(Vehicle.sr_ToStirngEndOfPairDelimiter);

            return stringBuilderResult.ToString();
        }

        private string enginCapacityToString()
        {
            StringBuilder stringBuilderResult = new StringBuilder();

            stringBuilderResult.Append(k_EnginCapacityKey);
            stringBuilderResult.Append(Vehicle.k_ToStirngPairDelimiter);
            stringBuilderResult.Append(m_EnginCapacity);
            stringBuilderResult.Append(Vehicle.sr_ToStirngEndOfPairDelimiter);

            return stringBuilderResult.ToString();
        }
    }
}
